<template>
  <div class="goodsinfo-container">
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>
    <!-- 商品轮播图区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :lunbotuList="lunbotu" :isfull="false"></swiper>
        </div>
      </div>
    </div>

    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{ goodsinfo.title }}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：
            <del>￥{{ goodsinfo.market_price }}</del>&nbsp;&nbsp;销售价：
            <span class="price_now">￥{{ goodsinfo.sell_price }}</span>
          </p>
          <p ref="box">
            购买数量：
            <numbox @getCount="getSelectedCount" :max="goodsinfo.stock_quantity"></numbox>
          </p>
          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button id="btn" type="danger" size="small" @click="addToCart">加入购物车</mt-button>
            <!-- 分析：如何实现加入购物车时候，拿到选择的数量 -->
            <!--  按钮属于goodsinfo，数字属于numbox 涉及到父子组件的嵌套-->
            <!-- 子组件向父组件传值，事件调用机制 -->
            <!-- 父向子传递方法，子调用这个方法，同时把数据当作参数传递给这个方法 -->
          </p>
        </div>
      </div>
    </div>

    <!-- 商品参数区域 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{ goodsinfo.goods_no }}</p>
          <p>库存情况： {{ goodsinfo.stock_quantity }}件</p>
          <p>上架时间：{{ goodsinfo.add_time | dateFormat }}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
        <!-- <br> -->
        <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>


<script>
import numbox from "../subcom/Goodsinfo_numbox.vue";
import swiper from "../subcom/swiper.vue";
export default {
  data() {
    return {
      id: this.$route.params.id, // 路由参数对象中的id挂载到data
      lunbotu: [],
      goodsinfo: [],
      ballFlag: false, // 控制小球的隐藏和显示的标识符
      selectedCount: 1 // 保证用户选中的商品数量，默认为1
    };
  },
  created() {
    this.getLunbotu();
    this.getGoodsInfo();
  },
  methods: {
    getLunbotu() {
      this.$http.get("api/getthumimages/" + this.id).then(result => {
        if (result.body.status === 0) {
          // 先循环轮播图数组的每一项，为item添加img属性
          result.body.message.forEach(item => {
            item.img = item.src;
          });
          this.lunbotu = result.body.message;
        }
      });
    },
    getGoodsInfo() {
      // 获取商品的信息
      this.$http.get("api/goods/getinfo/" + this.id).then(result => {
        if (result.body.status === 0) {
          this.goodsinfo = result.body.message[0];
        }
      });
    },
    goDesc(id) {
      // 点击使用编程式导航 跳转到 图文介绍页面
      this.$router.push({ name: "goodsdesc", params: { id } });
    },
    goComment(id) {
      // 点击使用编程式导航 跳转到 商品评论页面
      this.$router.push({ name: "goodscomment", params: { id } });
    },
    addToCart() {
      document.getElementById("btn").setAttribute("disabled", "disabled");
      setTimeout(function() {
        console.log("移除");
        document.getElementById("btn").removeAttribute("disabled");
      }, 1000);
      // 添加到购物车
      this.ballFlag = !this.ballFlag;
      var goodsinfo = {
        id: this.id,
        count: this.selectedCount,
        price: this.goodsinfo.sell_price,
        selected: true
        // shift + alt + f
      };
      this.$store.commit('addToCart', goodsinfo)
    },
    beforeEnter(el) {
      const boxPosition = this.$refs.box.getBoundingClientRect();
      // console.log(boxPosition)
      // 起始位置
      var ydist = boxPosition.y - 413;
      console.log(ydist);
      el.style.transform = "translate(0, " + ydist + "px)";
    },
    enter(el, done) {
      el.offsetWidth;
      // 小球动画优化思路
      // 1.先分析导致动画不准确的本质原因：我们把小球最终位移位置固定，分辨率或者滚动条移动，会出现问题

      // 2.动态计算坐标值  得出解题思路，徽标的横纵坐标，再得到小球的横纵坐标，让x，y值求差
      // 3.获取横纵坐标  domObject.getBoundingClientRect()
      const boxPosition = this.$refs.box.getBoundingClientRect();
      var dist = boxPosition.y - 413;
      // 获取小球在页面中的位置

      const ballPosition = this.$refs.ball.getBoundingClientRect();

      const badgePosition = document
        .getElementById("badge")
        .getBoundingClientRect();

      var xDistance = badgePosition.left - ballPosition.left;
      console.log(ballPosition.left, ballPosition.top, boxPosition.top);
      var yDistance = badgePosition.top - ballPosition.top;

      var xDist = badgePosition.left - ballPosition.left;
      var yDist = badgePosition.top - ballPosition.top + dist;
      console.log("yDist = " + yDist);

      // el.style.transform = `translate(${xDistance}px, ${yDistance}px)`;
      el.style.transform = `translate(${xDist}px, ${yDist}px)`;
      el.style.transition = "all 1s cubic-bezier(.4, -0.3, 1, .68)";
      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },
    getSelectedCount(count) {
      // 当子组件把选中的数量传递给父组件的时候，把选中的值保存到data上
      this.selectedCount = count;
      console.log("data from parent component " + this.selectedCount);
    }
  },
  components: {
    swiper,
    numbox
  }
};
</script>

<style lang="scss" scoped>
.goodsinfo-container {
  background-color: #eee;
  overflow: hidden;
}
.price_now {
  color: red;
  font-size: 16px;
  font-weight: bold;
}
.mui-card-footer {
  display: block;
  button {
    margin: 15px 0;
  }
}
.ball {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background-color: red;
  position: fixed;
  // position: absolute;
  z-index: 999;
  top: 410px;
  left: 146px;
}
</style>